/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wider.foundation.db;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Friendly class which can only be used within the DBx package. This class provides the abstraction of the query section indicated by
 * the query_id. 
 * @author William Choi <avextk@gmail.com>
 */
class DBxQuery {
    private boolean         viafast;
    private String          queryid;
    private String          nmspace;
    private DBxConn         dbxconn;
    private Element         domnode;
    private NodeList        stmtlst;

    DBxQuery(Element dbxconf, String query_id) throws ClassNotFoundException, IOException, SQLException{
        init(dbxconf, query_id);
    }

    final void init(Element dbxconf, String query_id) throws ClassNotFoundException, IOException, SQLException  {
        domnode = dbxconf.getOwnerDocument().getElementById(query_id);
        nmspace = dbxconf.getAttribute("namespace");
        queryid = domnode.getAttribute("id");
        dbxconn = DBxConn.newDBxConn(nmspace, dbxconf);
        stmtlst = domnode.getElementsByTagName("sqlstatement");
        viafast = false;
    }

    String getID(){
        return queryid;
    }
    
    long getCount(){
        return stmtlst.getLength();
    }

    String statement(int index){
        Element e = (Element) stmtlst.item((int) index);
        return e.getTextContent();
    }

    ResultSet execute(){
        return dbxconn.query(this);
    }


}
